<template>
	<view class="container">
		<view v-for="(item, index) in list" :key="index" class="item">
			<view>
				<u-image width="100%" height="460" :src="item.cover" border-radius="20"></u-image>
			</view>
			<view class="title">
				{{item.title}}
			</view>
			<view class="more">
				<view class="more-item">
					<u-icon name="clock"></u-icon>
					<view>{{$u.timeFormat(item.beginAt, 'yyyy年mm月dd日 hh:MM')}}</view>
				</view>
				
				<view class="more-item">
					<u-icon name="account"></u-icon>
					<view>{{item.visitorCount}}人已报名</view>
				</view>
			</view>
			
			<view class="action-view">
				<view class="staff">
					<u-image :src="item.staffHeadImg" width="40" height="40" border-radius="50%"></u-image>
					<view class="staffName">{{item.staffName}}</view>
				</view>
				<view v-if="item.createdBy === tchInfo.staff.id " class="btn" @click="bookingRecord(item, index)">
					记录
				</view>
				<view v-else class="btn" @click="bookingRecord(item, index)">
					{{item.booked === true ? '已预约' : '报名预约'}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getOtCourseList, bookingRecord } from '@/api/study.js'
	import pageMixin from '@/mixins/page.js'
	import { mapState } from 'vuex'
	export default {
		mixins: [pageMixin],
		data() {
			return {
				
			}
		},
		computed: {
			...mapState({
				tchInfo: state => state.user.tchInfo,
			}),
		},
		onLoad() {
			this.getList(this.page);	
		},
		methods: {

			getList(page) {
				const params = {
					pageNum: page + 1,
					pageSize: 10,
				}
				getOtCourseList(params).then(res => {
					console.log(res);
					// let authedMenuIds = res.data.content.map(x => x.title);
					// console.log(authedMenuIds);
					this.setData(res);
				})
			},
			
			bookingRecord(item, index) {
				if (item.createdBy === this.tchInfo.staff.id) {
					this.$u.route({
						url: '/pages_study/reservation-check-page',
						params: {
							offlineCourseId: item.id,
						}
					})
				} else {
					bookingRecord(item.id).then(res => {
						console.log(res);
						this.$u.toast('报名成功');
						item.booked = true;
						this.list.splice(index, item);
						console.log(this.list);
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: $app-page-bg-color;
	}
	.container{
		padding: 0 $app-page-padding;
	}
	
	.item{
		margin-top: 40rpx;
		margin-bottom: 40rpx;
		
		.title{
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #333333;
			margin-top: 25rpx;
		}
		
		.more{
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			font-size: 20rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #999999;
			margin-top: 10rpx;
			
			.more-item{
				display: flex;
				flex-direction: row;
			}
		}
		
		.action-view{
			margin-top: 20rpx;
			font-size: 18rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #6E645A;
			
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			
			.staff{
				display: flex;
				flex-direction: row;
				align-items: center;
				.staffName{
					margin-left: 10rpx;
				}
			}
			
			.btn{
				width: 218rpx;
				height: 88rpx;
				background: linear-gradient(0deg, #F5AC3C 0%, #AACD22 100%);
				border-radius: 44rpx;
				
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 38rpx;
			}
		}
	}
</style>
